/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-4-12
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import persist.APIDB;
import util.StringDealer;
import code.extract.CEConfig;
import dao.entity.APIEntity;

public class APIDao extends BaseDao{
	public static boolean insertAPI(Connection conn, String apiname){
		String sqlString = "insert into api (name) values(?)";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			pstmt.setString(1, StringDealer.getStringForMaxLength(125, apiname));
			pstmt.executeUpdate();
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public static int insertAPI(Connection conn, ArrayList<String> apiList){
		int okNum = 0;
		String sqlString = "insert into api (name) values(?)";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			for(int i=0; i<apiList.size(); ++i){
				pstmt.setString(1, StringDealer.getStringForMaxLength(125, apiList.get(i)));
				try{
					pstmt.executeUpdate();
					++ okNum;
				} catch (SQLException e1){
					e1.printStackTrace();
				}
			}
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return okNum;
	}
	public static boolean insertAPI(Connection conn, APIEntity api){
		if (api.getId() < 0){
			String sqlString = "insert into api (name) values(?)";
			PreparedStatement pstmt;
			try {
				pstmt = conn.prepareStatement(sqlString);
				pstmt.setString(1, StringDealer.getStringForMaxLength(125, api.getName()));
				pstmt.executeUpdate();
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
		}else{
			String sqlString = "insert into api (id,name) values(?,?)";
			PreparedStatement pstmt;
			try {
				pstmt = conn.prepareStatement(sqlString);
				pstmt.setInt(1, api.getId());
				pstmt.setString(2, StringDealer.getStringForMaxLength(125, api.getName()));
				pstmt.executeUpdate();
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	public static ArrayList<APIEntity> getAllAPI(Connection conn){
		ArrayList<APIEntity> resultList = new ArrayList<APIEntity>();
		String sqlString = "select id,name from api";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet resultSet = null;
			resultSet = stmt.executeQuery(sqlString);
			while (resultSet.next()){
				int thisId = resultSet.getInt(1);
				String thisName = resultSet.getString(2);
				APIEntity thisEntity = new APIEntity(thisId, thisName);
				resultList.add(thisEntity);
			}
			resultSet.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultList;
	}
	public static int getMaxID(Connection conn){
		int result = 0;
		String sqlString = "select max(id) from api";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet resultSet = null;
			resultSet = stmt.executeQuery(sqlString);
			if (resultSet.next()){
				result = resultSet.getInt(1);
			}
			resultSet.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	public static HashMap<String, Integer> getNameMapID(Connection conn){
		HashMap<String, Integer> result = new HashMap<String, Integer>();
		String sqlStirng = "select name,id from api";
		try {
			Statement stmt = conn.createStatement();
			ResultSet resultSet = stmt.executeQuery(sqlStirng);
			while(resultSet.next()){
				String thisName = resultSet.getString(1);
				int thisId = resultSet.getInt(2);
				result.put(thisName, thisId);
			}
			resultSet.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public static HashMap<Integer, String> getIDMapName(Connection conn){
		HashMap<Integer, String> result = new HashMap<Integer, String>();
		String sqlStirng = "select name,id from api";
		try {
			Statement stmt = conn.createStatement();
			ResultSet resultSet = stmt.executeQuery(sqlStirng);
			while(resultSet.next()){
				String thisName = resultSet.getString(1);
				int thisId = resultSet.getInt(2);
				result.put(thisId, thisName);
			}
			resultSet.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public static void initAPI(){
		//part 1, insert all api
		APIDB db = new APIDB(CEConfig.getAPIBDBDirname(), 1024 * 1024, "utf8");
		ArrayList<String> apiList = db.getAllAPI();
		Connection conn = getTestConn();
		System.out.println(insertAPI(conn, apiList));
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
	}
	
	public static void main(String args[]){

		
	}
}
